const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
const json = JSON.stringify(buf);

// 输出: {"type":"Buffer","data":[1,2,3,4,5]}
console.log(json);

const copy = JSON.parse(json, (key, value) => {
    return value && value.type === 'Buffer' ?
        Buffer.from(value.data) :
        value;
});

// 输出: <Buffer 01 02 03 04 05>
console.log(copy);

/////////////////// 缓冲区合并
let buffer1 = Buffer.from(('菜鸟教程'));
let buffer2 = Buffer.from(('www.runoob.com'));
let buffer3 = Buffer.concat([buffer1,buffer2]);

console.log("buffer3 内容: " + buffer3.toString());


/////////////// 缓冲区比较
let buffer4 = Buffer.from('ABC');
let buffer5 = Buffer.from('ABCD');
let result = buffer4.compare(buffer5);

if(result < 0) {
    console.log(buffer4 + " 在 " + buffer5 + "之前");
}else if(result == 0){
    console.log(buffer4 + " 与 " + buffer5 + "相同");
}else {
    console.log(buffer1 + " 在 " + buffer2 + "之后");
}